﻿//////////////////////////////////////////////////////////////////////////////
//
//  Copyright © 1998-2024 Glodon Company Limited.  All rights reserved.
//
//  Use of this software is subject to the terms of the Glodon license
//  agreement provided at the time of installation or download, or which
//  otherwise accompanies this software in either electronic or hard copy form.  
//
//////////////////////////////////////////////////////////////////////////////

#pragma once

#include "GcmpModelInterface.h"
#include "IObject.h"

namespace gcmp
{
    class UniIdentity;
    class INewParameterValidator;
    /// \brief 新参数验证器管理器
    ///
    /// 负责管理不序列化的新参数验证器，记录从ParameterDefinitionUid到INewParameterValidator的关系
    class GCMP_MODEL_INTERFACE_EXPORT IParameterValidatorsManager : public IObject
    {
        DEFINE_CAST_DERIVED(gcmp::IParameterValidatorsManager, gcmp::IObject);
    public:

        /// \brief 获取IParameterValidatorsManager对象
        /// \return 返回IParameterValidatorsManager对象
        static IParameterValidatorsManager* Get();

        /// \brief 通过ParameterDefinitionUid获得对应的参数验证器
        /// \param parameterDefinitionUid 给定的ParameterDefinitionUid
        /// \return 对应的参数验证器，没有则返回nullptr
        virtual const INewParameterValidator* GetParameterValidatorByParameterDefinitionUid(const UniIdentity& parameterDefinitionUid) const = 0;

        /// \brief 注册参数验证器
        /// \param parameterDefinitionUid 给定的ParameterDefinitionUid
        /// \param opValidator 对应的参数验证器
        /// \return 成功返回true，失败（如已存在）返回false
        virtual bool Register(const UniIdentity& parameterDefinitionUid, OwnerPtr<INewParameterValidator> opValidator) = 0;

        /// \brief 注销参数验证器
        /// \param parameterDefinitionUid 给定的ParameterDefinitionUid
        virtual void Unregister(const UniIdentity& parameterDefinitionUid) = 0;
    };

    // 注册时，构造函数就把需要的值设置上。
#define OneTimeRegisterParameterValidators(ParameterDefinitionUid, validator, ...)   \
    void Initialize##validator()   \
    {   \
        IParameterValidatorsManager* pParameterValidatorsManager = IParameterValidatorsManager::Get();   \
        DBG_WARN_AND_RETURN_VOID_UNLESS(pParameterValidatorsManager, L"pParameterValidatorsManager is empty", L"GMEP", L"20220104");   \
        DBG_WARN_AND_RETURN_VOID_UNLESS(pParameterValidatorsManager->Register(ParameterDefinitionUid, NEW_AS_OWNER_PTR(validator, __VA_ARGS__)),L"注册参数验证器失败", L"GMEP", L"20220104");   \
    }   \
    ONE_TIME_INITIALIZER(Initialize##validator);
}

